//
//  RootViewController.swift
//  SinaWeibo
//
//  Created by celeste on 16/5/11.
//  Copyright © 2016年 celeste. All rights reserved.
//

import UIKit
import HMRefresh
///根视图控制器-其他主控制器的基类
class RootViewController: UIViewController {
    
    // MARK : - 公共属性
    
    /// 表格式图
    var tableView :UITableView?
    // 刷新控件
    var refreshControl :HMRefreshControl?
    
    /// 访客视图
    var visitorView: VisitorView?
    // MARK : - 声明周期
    override func loadView() {
        
        //1.创建视图
        view = UIView()
        
        //2.用户登录显示tab,否则访客
        UserAccount.sharedUserAccount.isLogin ? setupTableView() : setupVisitorView()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if UserAccount.sharedUserAccount.isLogin {
            loadData()
        }else {
            NSNotificationCenter.defaultCenter().addObserver(self,
                selector: "loginSuccessed",
                name: LDWeiBoLoginSuccessedNotification,
                object: nil)
        }
        
    }
    
    
    /// 加载数据
    func loadData() {
        
        
        
    }
    @objc private func loginSuccessed() {
        
        view = nil
        NSNotificationCenter.defaultCenter().removeObserver(self,
            name: LDWeiBoLoginSuccessedNotification,
            object: nil)
    }
    
    
}

// MARK : - 代理
extension RootViewController : VisitorViewDelegate {
    
    func visitorViewDidRegister() {
        print("注册")
    }
    func visitorViewDidLogin() {
        let oauthVc = OAuthViewController()
        let nav = UINavigationController(rootViewController: oauthVc)
        presentViewController(nav, animated: true, completion: nil)
    }
}

extension RootViewController {
    //设置访客视图
    private func setupVisitorView() {
        
        visitorView = VisitorView()
        view.addSubview(visitorView!)
        
        visitorView?.snp_makeConstraints(closure: { (make) -> Void in
            
            make.edges.equalTo(view)
        })
        
        visitorView?.delegate = self
        // 设置状态栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册",
            style: .Plain,
            target: self,
            action: "visitorViewDidRegister")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录",
            style: .Plain,
            target: self,
            action: "visitorViewDidLogin")
        
    }
    
    private func setupTableView() {
        
        tableView = UITableView(frame: CGRectZero, style: .Plain)
        
        view.addSubview(tableView!)
        
        //自动布局
        tableView?.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(view)
        }
        
        tableView?.dataSource = self
        tableView?.delegate = self
        
    }
    //设置刷新控件
    //私有函数不能被继承,只是供内部使用
    func setupRefreshControl() {
        //如果tableview不存在直接返回
        
        guard let tableView = tableView else {
            return
        }
        //默认没有刷新控件
        if refreshControl != nil {
            return
        }
        refreshControl = HMRefreshControl()
        tableView.addSubview(refreshControl!)
        refreshControl?.pullingString = "放开起飞"
        refreshControl?.normalPullupString = "下拉起飞"
        refreshControl?.refreshingString = "正在起飞"
        refreshControl?.lastRefreshString = "末次 "
        
        refreshControl?.pulldownView = LDPulldownRefreshView.refreshView()
        
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
    }
    
    
}

// MARK : - 数据源方法
extension RootViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 0
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        
        return UITableViewCell()
        
    }
    
}

